<?php

namespace app\api\controller;

use app\common\controller\ApiBase;
use think\Controller;
use think\Request;
use app\common\WeChat;
class User extends ApiBase
{
    protected $user_id = null;
    protected $userinfo;

    public function _initialize()
    {
        $user_id = input("user_id");
        $user = model("Users")->get(["id" => $user_id]);
        $action=Request::instance()->action();
        $actionlist = get_class_methods(__CLASS__ );
        if(!in_array($action,$actionlist)){
            exit(json_encode([self::ERROR_DATA, "非法操作"],JSON_UNESCAPED_UNICODE));
        }
        if (!$user){
            exit(json_encode([self::ERROR_DATA, "非法用户"],JSON_UNESCAPED_UNICODE));
        }
        $this->user_id = $user_id;
    }

    /**
     * 登录
     * @return \think\Response
     */
    public function login()
    {
        $data=[
            'code'=>input('code'),
            'iv'=>input('iv'),
            'encryptedData'=>input('encryptedData')
        ];
        $res=$this->validate([
            ['code','require','code参数错误'],
            ['iv','require','iv参数错误'],
            ['encryptedData','require','iv参数错误'],
        ],$data);
        $wechat= new \WeChat\WeChat();
        if($res!==true) return $this->ApiReture(self::ERROR_DATA,$res);
        $info=$wechat->getOpenId($data['code']);
        if(!$info) return $this->ApiReture(self::ERROR_DATA,$wechat->errMsg);
        $user_info=$wechat->getUserInfo($info['session_key'],$data['encryptedData'],$data['iv']);
        if(!$user_info) return $this->ApiReture(self::ERROR_DATA,'获取信息失败请重试');
        $user=\app\common\model\User::get(['open_id'=>$user_info['openId']]);
        $data=[
            'nickname'=>$user_info['nickName'],
            'avatar'=>$user_info['avatarUrl'],
            'gender'=>$user_info['gender'],
            'province'=>$user_info['province'],
            'city'=>$user_info['city'],
            'union_id'=>isset($user_info['unionId'])?$user_info['unionId']:'',
            'open_id'=>$user_info['openId']
        ];
        if(!$user){
            $data['access_token']=RandNumber32();
            $user=\app\common\model\User::create($data);
        }else{
            $user->save($data);
        }
        $data=[
            'nickname'=>$user['nickname'],
            'avatar_url'=>$user['avatar_url'],
            'gender'=>$user['gender'],
            'access_token'=>$user['access_token']
        ];
        return $this->ApiReture(self::SUCCESS_DATA,'获取成功',$data);
    }

    /**
     * 意见反馈接口
     */
    public function feebback()
    {
        $feedback_detail = input("feedback_detail");
        if (!$feedback_detail) return $this->ApiReture(self::ERROR_DATA, '反馈内容不能为空');
        $data['uid'] = $this->user_id;
        $data['feedback_detail'] = $feedback_detail;
        $res = model("Feedback")->save($data);
        if ($res !== false) {
            return $this->ApiReture(self::SUCCESS_DATA, [], "提交成功");
        } else {
            return $this->ApiReture(self::ERROR_DATA, [], "提交失败，请重试！");
        }
    }
}
